package com.joysuch.wwyt.api.base.controller;

import com.joysuch.wwyt.api.base.vo.RiskDistrictVo;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.risk.bean.RiskDistrictListSearchBean;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.service.RiskDistrictService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "双预防-风险分区")
@RequestMapping("/api/base/district")
public class DistrictController {

    private static final Logger logger = LoggerFactory.getLogger(DistrictController.class);

    @Autowired
    private RiskDistrictService districtService;

    @RequestMapping("list")
    public ResultBean workFlowPage(Pageable page, RiskDistrictListSearchBean condition) {
        try {
            page = PageDataUtils.addOrderByDesc(page, "createTime");
            Page<RiskDistrict> district = districtService.findByPage(page, condition);
            List<RiskDistrictVo> list = new ArrayList<>(district.getContent().size());
            for (RiskDistrict each : district) {
                RiskDistrictVo districtVo = new RiskDistrictVo();
                BeanUtils.copyProperties(each, districtVo);
                list.add(districtVo);
            }
            return ResultBean.pageData(list, district.getTotalElements());
        } catch (Exception e) {
            logger.error("查询区域列表失败", e);
            return ResultBean.fail(101, "查询区域列表失败");
        }
    }

}
